package com.techventus.locations;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
public class PowerMode extends Activity{
	
	Button cancelButton;
	Button confirmButton;
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.locationmode);
	    cancelButton = (Button) this.findViewById( R.id.cancelPowerChange);
	    cancelButton.setOnClickListener(closeClick);

	    Spinner powerSpin = (Spinner) findViewById(R.id.powerspinner);
	    ArrayAdapter powerAdapter = ArrayAdapter.createFromResource(
	            this, R.array.power, android.R.layout.simple_spinner_item);
	    powerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    powerSpin.setAdapter(powerAdapter);
	    
	    
	    Spinner accuracySpin = (Spinner) findViewById(R.id.accuracyspinner);
	    ArrayAdapter accuracyAdapter = ArrayAdapter.createFromResource(
	            this, R.array.accuracy, android.R.layout.simple_spinner_item);
	    accuracyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    accuracySpin.setAdapter(accuracyAdapter);
	    
	    
	    String[] powac = getPowerAccuracy();
	    
	    if(powac[0].equals("POWER_LOW")){
	    	powerSpin.setSelection(0);
	    }else if(powac[0].equals("POWER_MEDIUM")){
	    	powerSpin.setSelection(1);
	    }else if(powac[0].equals("POWER_HIGH")){
	    	powerSpin.setSelection(2);
	    }else if(powac[0].equals("NO_REQUIREMENT")){
	    	powerSpin.setSelection(3);
	    }
	    
	    
	    if(powac[1].equals("ACCURACY_COURSE")){
	    	accuracySpin.setSelection(0);
	    }else if(powac[1].equals("ACCURACY_FINE")){
	    	accuracySpin.setSelection(1);
	    }
	    
	    
	    confirmButton = (Button) this.findViewById( R.id.confirmPowerChange);
	    confirmButton.setOnClickListener(confirmClick(powerSpin,accuracySpin));
	}
	
	
	OnClickListener closeClick = new OnClickListener(){

		@Override
		public void onClick(View arg0) {
			PowerMode.this.finish();
		}
		
	};
	
	OnClickListener confirmClick(final Spinner powerSpin, final Spinner accuracySpin){
	
		OnClickListener ocl = new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				System.out.println("Confirm Button Click");
				System.out.println(powerSpin.getAdapter().getItem(powerSpin.getSelectedItemPosition()));
				System.out.println(accuracySpin.getAdapter().getItem(accuracySpin.getSelectedItemPosition()));
				try{
			       SQLiteDatabase sql =  openOrCreateDatabase("db",0,null);
			        sql.execSQL("UPDATE STATUS SET value = '"+powerSpin.getAdapter().getItem(powerSpin.getSelectedItemPosition())+"' WHERE KEY = 'power';");
			        //SQLHelper.listPhoneAndLocationPhoneEnable(sql);
			        sql.execSQL("UPDATE STATUS SET value = '"+accuracySpin.getAdapter().getItem(accuracySpin.getSelectedItemPosition())+"' where key = 'accuracy';");
			        sql.close();
				}catch(Exception e){
					e.printStackTrace();
				}
				PowerMode.this.finish();
				
			}
			
		};
		return ocl;
	}
	
	
	String[] getPowerAccuracy(){
		String power = "";
		String accuracy = "";
		try{
			SQLiteDatabase sql =  openOrCreateDatabase("db",0,null);
			Cursor c = sql.rawQuery("SELECT * FROM STATUS WHERE key = 'power';", null);
			while(c.moveToNext()){
				 power = c.getString(2);
				 System.out.println(power);
			}
			c.close();
			c  = sql.rawQuery("SELECT * FROM STATUS WHERE key = 'accuracy';", null);
			while(c.moveToNext()){
				 accuracy = c.getString(2);
				 System.out.println(accuracy);
			}
			c.close();
			sql.close();
		}catch(Exception e){
			e.printStackTrace();
		}
		String[] ret = {power,accuracy};
		return ret;
		 //  sql.execSQL("UPDATE SETTINGS SET VALUE = '"+powerSpin.getAdapter().getItem(powerSpin.getSelectedItemPosition())+"' WHERE KEY = 'POWER';");
	        //SQLHelper.listPhoneAndLocationPhoneEnable(sql);
	       // sql.close();
	}
}
